package com.njtoyo.taxi.admin.library;

import com.njtoyo.taxi.admin.rest.presenter.data.data_analysis.DriverDataPresenter;

import java.math.BigDecimal;
import java.util.List;
import java.util.function.Function;

public class CommUtil {

    public static <R> R listToResult(List<DriverDataPresenter> list, Long id, Function<DriverDataPresenter, R> field, R r) {
        return list.stream()
                .filter(e -> e.getDriverId().equals(id))
                .findAny()
                .map(field)
                .orElse(r);
    }

    public static double div(int c, int p) {
        if (p == 0) {
            return 0.0;
        }
        return (double) c / (double) p;
    }

    public static String rateStr(double s) {
        BigDecimal bigDecimal = new BigDecimal(s * 100);
        return bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).toString() + "%";
    }
}
